package com.bysc.cmsservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.cmsservice.entity.CmsMember;
import com.bysc.cmsservice.entity.vo.CmsMemberQueryVo;
import com.bysc.cmsservice.service.CmsMemberService;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.commonutils.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.bysc.commonutils.constant.RedisConstant.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/cmsservice/cms-member")
@Api(tags = "成员管理")
@Slf4j
public class CmsMemberController {

    @Autowired
    private CmsMemberService cmsMemberService;


    /*
     * 第三方管理员更新优秀成员信息
     * */
    @ApiOperation(value = "新增或更新成员（仅后台）")
    @PostMapping("addOrUpdateMember")
    public Results addOrUpdateMember(
            @ApiParam(name = "cmsMember", value = "成员信息", required = true)
            @RequestBody CmsMember cmsMember, HttpServletRequest request) {
        try {
            String TOKEN_clubId= JwtUtils.getClubIdByJwtToken(request);
            if(!StringUtils.isEmpty(TOKEN_clubId)){
                cmsMember.setClubId(TOKEN_clubId);
            }
            cmsMemberService.saveOrUpdate(cmsMember);
            return Results.ok().data("member", cmsMember).message(MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /*
    * 可带条件分页查询优秀成员
    * 新增根据clubId查询
    * */
    @ApiOperation(value = "可带条件分页查询优秀成员（前后台通用）")
    @PostMapping("getMemberList/{page}/{limit}")
    public Results getMemberList(
            @ApiParam(name = "page", value = "查询页数", required = true) @PathVariable Integer page,
            @ApiParam(name = "limit", value = "查询数量", required = true) @PathVariable Integer limit,
            @ApiParam(name = "queryVo", value = "查询条件", required = false) @RequestBody(required = false) CmsMemberQueryVo queryVo, HttpServletRequest request) {
        try {
            Page<CmsMember> pageParam = new Page<>(page, limit);
            LambdaQueryWrapper<CmsMember> wrapper = new LambdaQueryWrapper<CmsMember>()
                    .orderByDesc(CmsMember::getSort);
            if (queryVo != null) {
                String id = queryVo.getId();
                String name = queryVo.getName();
                String wxNumber = queryVo.getWxNumber();
                String tagFirst = queryVo.getTagFirst();
                String tagSecond = queryVo.getTagSecond();
                String description = queryVo.getDescription();
                String clubId= queryVo.getClubId();
                if(!StringUtils.isEmpty(clubId)){
                    wrapper.eq(CmsMember::getClubId, clubId);
                }
                if (!StringUtils.isEmpty(clubId)) {
                    wrapper.eq(CmsMember::getClubId, clubId);
                }
                if (!StringUtils.isEmpty(id)) {
                    wrapper.eq(CmsMember::getId, id);
                }
                if (!StringUtils.isEmpty(wxNumber)) {
                    wrapper.like(CmsMember::getWxNumber, wxNumber);
                }
                if (!StringUtils.isEmpty(name)) {
                    wrapper.like(CmsMember::getName, name);
                }
                if (!StringUtils.isEmpty(tagFirst)) {
                    wrapper.like(CmsMember::getTagFirst, tagFirst);
                }
                if (!StringUtils.isEmpty(tagSecond)) {
                    wrapper.like(CmsMember::getTagSecond, tagSecond);
                }
                if (!StringUtils.isEmpty(description)) {
                    wrapper.like(CmsMember::getDescription, description);
                }
            }
            String TOKEN_clubId = JwtUtils.getClubIdByJwtToken(request);
            if(!StringUtils.isEmpty(TOKEN_clubId)){
                wrapper.eq(CmsMember::getClubId, TOKEN_clubId);
            }
            IPage<CmsMember> cmsMembers = cmsMemberService.page(pageParam, wrapper);
            return Results.ok().data("list", cmsMembers).message(MessageConstant.QUERY_MEMBER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.QUERY_MEMBER_FAIL);
        }

    }


    @ApiOperation("删除优秀成员（后台前端传clubId）")
    @DeleteMapping("deleteMemberById/{id}")
    public Results deleteMemberById(
            @ApiParam(name = "id", value = "成员id", required = true)
            @PathVariable("id") String id) {
        try {
            cmsMemberService.removeById(id);
            return Results.ok().message(MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }




}

